package com.gin.no_gay_alliance.param;

import android.os.Parcel;
import android.os.Parcelable;

import com.gin.jackson.utils.ObjectUtils;
import com.gin.nga.enums.SubForumType;
import com.gin.nga.response.field.Forum;
import com.gin.nga.response.field.SubForum;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 搜索区域：包括版面和合集
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/8/9 15:18
 **/
public class SearchArea implements Parcelable {
    public static final Creator<SearchArea> CREATOR = new Creator<SearchArea>() {
        @Override
        public SearchArea createFromParcel(Parcel in) {
            return new SearchArea(in);
        }

        @Override
        public SearchArea[] newArray(int size) {
            return new SearchArea[size];
        }
    };
    /**
     * 类型
     */
    public String type;
    /**
     * id
     */
    public Long id;
    /**
     * 名称
     */
    public String name;
    /**
     * 是否选中
     */
    public boolean checked;

    public SearchArea() {
    }

    public SearchArea(Forum forum) {
        if (forum.getColTid() != null) {
            this.type = SubForumType.COL.name;
            this.id = forum.getColTid();
            this.name = forum.getColTitle();
        } else {
            this.type = SubForumType.FORUM.name;
            this.id = forum.getForumId();
            this.name = forum.getForumName();
        }
    }

    public SearchArea(SubForum subForum) {
        this.type = subForum.getType().name;
        this.id = subForum.getId();
        this.name = subForum.getName();
    }

    public SearchArea(SubForumType type, Long id, String name) {
        this.type = type.name;
        this.id = id;
        this.name = name;
    }

    protected SearchArea(Parcel in) {
        type = in.readString();
        if (in.readByte() == 0) {
            id = null;
        } else {
            id = in.readLong();
        }
        name = in.readString();
        checked = in.readByte() != 0;
    }

    public static List<SearchArea> build(Forum forum) {
        ArrayList<SearchArea> list = new ArrayList<>();
        list.add(new SearchArea(forum));
        if (forum.getColTid() == null) {
            // 当前为版面，返回当前版面+子版面
            List<SubForum> subForums = forum.getSubForums();
            if (!ObjectUtils.isEmpty(subForums)) {
                List<SearchArea> areas = subForums.stream()
                        .filter(i -> i.getType() == SubForumType.FORUM)
                        .map(SearchArea::new).collect(Collectors.toList());
                list.addAll(areas);
            }
        }
        // 将第一项选中
        list.get(0).checked = true;
        return list;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(type);
        if (id == null) {
            dest.writeByte((byte) 0);
        } else {
            dest.writeByte((byte) 1);
            dest.writeLong(id);
        }
        dest.writeString(name);
        dest.writeByte((byte) (checked ? 1 : 0));
    }

}
